// pages/goodsDetail/groupList/groupList.js
const goodsService = require('../../../service/goodsService.js')
const utils = require('../../../utils/util.js');
// load promise 
require('../../../utils/wx-pro.js');
var timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupList: [],
    page: {
      pageNo: 1,
      pageSize: 15
    },
    totalCount: 0,
    goodsId: '',
    shopId: '',
    reqDelay: false,
    isEmpty: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.goodId)
    var goodsId = options.goodId
    var shopId = options.shopId
    this.setData({
      goodsId: goodsId,
      shopId: shopId
    })
  },

  onShow: function(){
    this.setData({
      groupList: [],
      page: {
        pageNo: 1,
        pageSize: 15
      },
      isEmpty: true
    })
    this.getGroupList({ ptGoodsId: this.data.goodsId, shopId: this.data.shopId })
  },

  setTimer() {
    timer = setInterval(() => {
      var list = this.data.groupList
      for (var i = 0; i < list.length; i++) {
        if (list[i].remain > 1000) {
          var remain = "groupList[" + i + "].remain"
          var remainFormat = "groupList[" + i + "].remainFormat"
          this.setData({
            [remain]: list[i].remain - 1000,
            [remainFormat]: this.secondToDate(list[i].remain / 1000)
          })
        } else {
          var remainFormat = "groupList[" + i + "].remainFormat"
          this.setData({
            [remainFormat]: ''
          })
        }
      }
    }, 1000)
  },
  secondToDate(result) {
    var h = Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60));
    if (h > 0) {
      return result = h + "小时" + m + "分钟" + s + "秒";
    } else {
      return result = m + "分钟" + s + "秒";
    }
  },
  clearTimer() {
    timer = clearInterval(timer)
  },
  getGroupList(params) {
    this.setData({
      reqDelay: true
    });
    params.pageNo = 1
    params.pageSize = 15
    goodsService.listXcxOpenGroup(params).then(data => {
      if (data.code == 200) {
        this.setData({
          totalPage: data.response.page.totalPage
        })
        var array = data.response.xcxOpenGroupList
        for (var i of array) {
          i.remain = i.endTime - data.timestamp
          i.remainFormat = ''
        }
        var totalList = {}
        //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
        if (!this.data.isEmpty) {
          totalList = this.data.groupList.concat(array);
        }
        else {
          totalList = array;
          this.data.isEmpty = false;
        }
        this.setData({
          groupList: totalList,
          totalCount: data.response.page.totalCount
        })
        this.clearTimer()
        this.setTimer()
        wx.hideNavigationBarLoading();
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch((e) => {
        console.log(e)
      })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  getMoreDate: function () {
    if (!this.data.reqDelay) {
      if (this.data.pageParam.pageNo >= this.data.totalPage) {
        console.log("没有了")
      } else {
        console.log(2222222222)
        wx.showNavigationBarLoading()
        this.setData({
          pageParam: {
            pageNo: this.data.pageParam.pageNo + 1,
            pageSize: 15
          },
        })
        this.getGroupList()
      }
    }
  }
  
})